home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / osrc.arc / NRS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-15  |  6.1 KB  |  265 lines

  1. /* This module implements the serial line framing method used by
  2.  * net/rom nodes.  This allows the net/rom software to talk to
  3.  * an actual net/rom over its serial interface, which is useful
  4.  * if we want to do packet switching for multi-line wormholes.
  5.  * Dan Frank, W9NK
  6.  */
  7. #include <stdio.h>
  8. #include "global.h"
  9. #include "mbuf.h"
  10. #include "iface.h"
  11. #include "ax25.h"
  12. #include "nrs.h"
  13. #include "asy.h"
  14. #include "trace.h"
  15.  
  16.  
  17. extern struct mbuf *Hopper;
  18. static struct mbuf *nrs_encode();
  19. static int nrsq();
  20. static void nrasy_start();
  21.  
  22. /* control structures, sort of overlayed on async control blocks */
  23. struct nrs Nrs[ASY_MAX];
  24.  
  25. /* Send a raw net/rom serial frame */
  26. nrs_raw(iface,bp)
  27. struct iface *iface;
  28. struct mbuf *bp;
  29. {
  30.     dump(iface,IF_TRACE_OUT,TYPE_AX25,bp) ;
  31.     
  32.     /* Queue a frame on the output queue and start transmitter */
  33.     nrsq(iface->dev,bp);
  34. }
  35.  
  36. /* Encode a raw packet in net/rom framing, put on link output queue, and kick
  37.  * transmitter
  38.  */
  39. static int
  40. nrsq(dev,bp)
  41. int16 dev;        /* Serial line number */
  42. struct mbuf *bp;    /* Buffer to be sent */
  43. {
  44.     register struct nrs *sp;
  45.  
  46.     if((bp = nrs_encode(bp)) == NULLBUF)
  47.         return;    
  48.  
  49.     sp = &Nrs[dev];
  50.     enqueue(&sp->sndq,bp);
  51.     sp->sndcnt++;
  52.     if(sp->tbp == NULLBUF)
  53.         nrasy_start(dev);
  54. }
  55.  
  56. /* Start output, if possible, on asynch device dev */
  57. static void
  58. nrasy_start(dev)
  59. int16 dev;
  60. {
  61.     register struct nrs *sp;
  62.  
  63.     if(!stxrdy(dev))
  64.         return;        /* Transmitter not ready */
  65.  
  66.     sp = &Nrs[dev];
  67.     if(sp->tbp != NULLBUF){
  68.         /* transmission just completed */
  69.         free_p(sp->tbp);
  70.         sp->tbp = NULLBUF;
  71.     }
  72.     if(sp->sndq == NULLBUF)
  73.         return;    /* No work */
  74.  
  75.     sp->tbp = dequeue(&sp->sndq);
  76.     sp->sndcnt--;
  77.     asy_output(dev,sp->tbp->data,sp->tbp->cnt);
  78. }
  79.  
  80. /* Encode a packet in net/rom serial format */
  81. static struct mbuf *
  82. nrs_encode(bp)
  83. struct mbuf *bp;
  84. {
  85.     struct mbuf *lbp;    /* Mbuf containing line-ready packet */
  86.     register char *cp;
  87.     char c;
  88.     unsigned char csum = 0 ;
  89.  
  90.     /* Allocate output mbuf that's twice as long as the packet.
  91.      * This is a worst-case guess (consider a packet full of STX's!)
  92.      * Add five bytes for STX, ETX, checksum, and two nulls.
  93.      */
  94.     lbp = alloc_mbuf(2*len_mbuf(bp) + 5);
  95.     if(lbp == NULLBUF){
  96.         /* No space; drop */
  97.         free_p(bp);
  98.         return NULLBUF;
  99.     }
  100.     cp = lbp->data;
  101.  
  102.     *cp++ = STX ;
  103.  
  104.     /* Copy input to output, escaping special characters */
  105.     while(pullup(&bp,&c,1) == 1){
  106.         switch(uchar(c)){
  107.         case STX:
  108.         case ETX:
  109.         case DLE:
  110.             *cp++ = DLE;
  111.             /* notice drop through to default */
  112.         default:
  113.             *cp++ = c;
  114.         }
  115.         csum += uchar(c) ;
  116.     }
  117.     *cp++ = ETX;
  118.     *cp++ = csum ;
  119.     *cp++ = NUL ;
  120.     *cp++ = NUL ;
  121.     
  122.     lbp->cnt = cp - lbp->data;
  123.     return lbp;
  124. }
  125. /* Process incoming bytes in net/rom serial format
  126.  * When a buffer is complete, return it; otherwise NULLBUF
  127.  */
  128. static
  129. struct mbuf *
  130. nrs_decode(dev,c)
  131. int16 dev;    /* net/rom unit number */
  132. char c;        /* Incoming character */
  133. {
  134.     struct mbuf *bp;
  135.     register struct nrs *sp;
  136.  
  137.     sp = &Nrs[dev];
  138.     switch(sp->state) {
  139.         case NRS_INTER:
  140.             if (uchar(c) == STX) {    /* look for start of frame */
  141.                 sp->state = NRS_INPACK ;    /* we're in a packet */
  142.                 sp->csum = 0 ;                /* reset checksum */
  143.             }
  144.             return NULLBUF ;
  145.             break ;    /* just for yucks */
  146.         case NRS_CSUM:
  147.             bp = sp->rbp ;
  148.             sp->rbp = NULLBUF ;
  149.             sp->rcnt = 0 ;
  150.             sp->state = NRS_INTER ;    /* go back to inter-packet state */
  151.             if (sp->csum == uchar(c)) {
  152.                 sp->packets++ ;
  153.                 return bp ;
  154.             }
  155.             else {
  156.                 free_p(bp) ;    /* drop packet with bad checksum */
  157.                 sp->errors++ ;    /* increment error count */
  158.                 return NULLBUF ;
  159.             }
  160.             break ;
  161.         case NRS_ESCAPE:
  162.             sp->state = NRS_INPACK ;    /* end of escape */
  163.             break ;            /* this will drop through to char processing */
  164.         case NRS_INPACK:
  165.             switch (uchar(c)) {
  166.                 /* If we see an STX in a packet, assume that previous */
  167.                 /* packet was trashed, and start a new packet */
  168.                 case STX:
  169.                     free_p(sp->rbp) ;
  170.                     sp->rbp = NULLBUF ;
  171.                     sp->rcnt = 0 ;
  172.                     sp->csum = 0 ;
  173.                     sp->errors++ ;
  174.                     return NULLBUF ;
  175.                     break ;
  176.                 case ETX:
  177.                     sp->state = NRS_CSUM ;    /* look for checksum */
  178.                     return NULLBUF ;
  179.                     break ;
  180.                 case DLE:
  181.                     sp->state = NRS_ESCAPE ;
  182.                     return NULLBUF ;
  183.                     break ;
  184.             }
  185.     }
  186.     /* If we get to here, it's with a character that's part of the packet.
  187.      * Make sure there's space for it.
  188.      */
  189.     if(sp->rbp == NULLBUF){
  190.         /* Allocate first mbuf for new packet */
  191.         if((sp->rbp1 = sp->rbp = alloc_mbuf(NRS_ALLOC)) == NULLBUF) {
  192.             sp->state = NRS_INTER ;
  193.             return NULLBUF; /* No memory, drop */
  194.         }
  195.         sp->rcp = sp->rbp->data;
  196.     } else if(sp->rbp1->cnt == NRS_ALLOC){
  197.         /* Current mbuf is full; link in another */
  198.         if((sp->rbp1->next = alloc_mbuf(NRS_ALLOC)) == NULLBUF){
  199.             /* No memory, drop whole thing */
  200.             free_p(sp->rbp);
  201.             sp->rbp = NULLBUF;
  202.             sp->rcnt = 0;
  203.             sp->state = NRS_INTER ;
  204.             return NULLBUF;
  205.         }
  206.         sp->rbp1 = sp->rbp1->next;
  207.         sp->rcp = sp->rbp1->data;
  208.     }
  209.     /* Store the character, increment fragment and total
  210.      * byte counts
  211.      */
  212.     *sp->rcp++ = c;
  213.     sp->rbp1->cnt++;
  214.     sp->rcnt++;
  215.     sp->csum += uchar(c) ;    /* add to checksum */
  216.     return NULLBUF;
  217. }
  218.  
  219. /* Process net/rom serial line I/O */
  220. void
  221. nrs_recv(dev)
  222. int dev;
  223. {
  224.     char c;
  225.     struct mbuf *bp,*nbp;
  226.     struct phdr *phdr;
  227.  
  228.     /* Process any pending input */
  229.     for(;;){
  230.         c = get_asy(dev);
  231.         if((bp = nrs_decode(dev,c)) == NULLBUF)
  232.             continue;
  233.         if((nbp = pushdown(bp,sizeof(struct phdr))) == NULLBUF){
  234.             free_p(bp);
  235.             continue;
  236.         }
  237.         phdr = (struct phdr *)nbp->data;
  238.         phdr->iface = Nrs[dev].iface;
  239.         phdr->type = TYPE_AX25;
  240.         enqueue(&Hopper,nbp);
  241.         /* Kick the transmitter if it's idle */
  242.         if(stxrdy(dev))
  243.             nrasy_start(dev);
  244.     }
  245.  
  246. }
  247. /* donrstat:  display status of active net/rom serial interfaces */
  248. donrstat(argc,argv)
  249. int argc ;
  250. char *argv[] ;
  251. {
  252.     register struct nrs *np ;
  253.     register int i ;
  254.  
  255.     printf("Interface  SndQ  RcvB  NumReceived  CSumErrors\n") ;
  256.  
  257.     for (i = 0, np = Nrs ; i < ASY_MAX ; i++, np++)
  258.         if (np->iface != NULLIF)
  259.             printf(" %8s   %3d  %4d   %10lu  %10lu\n",
  260.                     np->iface->name, np->sndcnt, np->rcnt,
  261.                     np->packets, np->errors) ;
  262.  
  263.     return 0 ;
  264. }
  265.